import 'package:flutter/material.dart';
import 'package:homepad/logger/logger.dart';
import 'package:homepad/iot/basic_node.dart';
import 'package:homepad/models/nova_model.dart';
import 'package:homepad/views/item_service_card.dart';
import 'package:provider/provider.dart';

class PanelService extends StatelessWidget {
  final GestureTapCallback? onCancel;

  const PanelService({super.key, this.onCancel});

  final titleStyle = const TextStyle(
    color: Colors.white,
    fontSize: 19,
    fontFamily: "PingFangLight",
    letterSpacing: 0.45,
  );

  @override
  Widget build(BuildContext context) {
    List<BasicNode> nodes = [];
    for (var item in context.watch<NovaModel>().config.nodes) {
      logger.info("name: ${item.name} icon:${item.icon}");
      if (item.icon != null) {
        nodes.add(item);
      }
    }

    return Scaffold(
        appBar: AppBar(
          leadingWidth: 20,
          title: Text("设备列表", style: titleStyle.copyWith(fontSize: 20)),
          backgroundColor: Colors.white10,
          primary: true,
          actions: [
            TextButton(
              onPressed: onCancel,
              child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 25, vertical: 10),
                  child: Text("返回", style: titleStyle.copyWith(color: Colors.redAccent))),
            ),
            const SizedBox(width: 5),
          ],
        ),
        backgroundColor: Colors.black,
        body: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 20),
              height: 500,
              child: GridView.count(
                crossAxisCount: 4,
                padding: const EdgeInsets.symmetric(horizontal: 40),
                mainAxisSpacing: 15,
                crossAxisSpacing: 15,
                childAspectRatio: 2.25,
                scrollDirection: Axis.vertical,
                children: nodes.map<Widget>((item) {
                  return ItemServiceCard(node: item);
                }).toList(),
              ),
            ),
          ],
        ));
  }
}
